import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.xxx.util.CommonUtil;

/**
 * 生成PDF报告
 */
public class PDFReport {
 
    public static void main(String[] args) throws Exception, DocumentException {
    	System.out.println("生成PDF文档开始");
    	// PO#列
        List<String> po=new ArrayList<String>();
        add(po, "po", 100);
        // Line#列
        List<String> line=new ArrayList<String>();
        add(line, "line", 100);
        // Part#列
        List<String> part=new ArrayList<String>();
        add(part, "part", 100);
        // Description#列
        List<String> description=new ArrayList<String>();
        add(description, "description", 100);
        // Origin#列
        List<String> origin=new ArrayList<String>();
        add(origin, "origin", 100);
         
        // 创建Document
        Document document=new Document();
         
        // 添加字体
        BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
         
        Font headerFont=new Font(bfChinese,8, Font.BOLD); // header部分字体大小
        Font titleFont=new Font(bfChinese, 8, Font.NORMAL); // title部分字体大小
        Font bodyFont=new Font(bfChinese, 8, Font.NORMAL); // body部分字体大小
         
        // 创建PdfWriter实例，并生成pdf文档
        PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\进货单.pdf")));
         
        document.open();
         
        // 计算分页参数
        int recordPerPage=50; // 分页大小
        int fullPageRequired=po.size()/recordPerPage; // 正常分页数量
        int remainPage=po.size()%recordPerPage>1?1:0; // 剩余分页数量
        int totalPage=fullPageRequired+remainPage; // 总页数
         
        for(int j=0;j<totalPage;j++){
            document.newPage(); // 新建一个page
             
            // 添加分页信息
            String pageNo=CommonUtil.leftPad("页码: "+(j+1)+" / "+totalPage, 615);
            Paragraph pageNumber=new Paragraph(pageNo, headerFont) ;
            document.add(pageNumber);
             
            // 添加图片
            Image jpeg=Image.getInstance("D:\\title.jpg"); // 需要提前准备好图片素材
            jpeg.setAlignment(Image.ALIGN_CENTER);
            jpeg.scaleAbsolute(530, 37);
            document.add(jpeg);
             
            // 创建一个2列的table Header部分
            PdfPTable tHeader=new PdfPTable(4);
            float[] widthsHeader={1f, 3f, 1f, 3f};
            tHeader.setWidths(widthsHeader); // 设置相对宽度
            tHeader.setTotalWidth(100f);
            tHeader.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
             
            String compAdd="河源市高新技术开发区兴业大道中66号";
            String company="丰达音响（河源）有限公司";
            String vendor="V006";
            String vendorName="中山市卢氏五金有限公司";
            String ccn="FHH";
            String mas_loc="FHH";
            String delivery_note="20130718001";
            String receive_date="20130718";
            String dept="H11";
            String asn="0123456789";
            
            PdfPCell c1Header=new PdfPCell(new Paragraph("地址：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(compAdd, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("供应商：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(vendor, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("公司：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(company, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("供应商工厂：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(vendorName, titleFont));
            tHeader.addCell(c1Header);
            c1Header = new PdfPCell(new Paragraph("CCN：", headerFont));
            tHeader.addCell(c1Header);
            c1Header = new PdfPCell(new Paragraph(ccn, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("Master Loc：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(mas_loc, titleFont));
            tHeader.addCell(c1Header);
            c1Header = new PdfPCell(new Paragraph("送货编号：", headerFont));
            tHeader.addCell(c1Header);
            c1Header = new PdfPCell(new Paragraph(delivery_note, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("送货日期：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(receive_date, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("Dept：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(dept, titleFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph("ASN：", headerFont));
            tHeader.addCell(c1Header);
            c1Header=new PdfPCell(new Paragraph(asn, titleFont));
            tHeader.addCell(c1Header);
            document.add(tHeader);
             
            // 创建一个5列的数据记录table部分
            PdfPTable t=new PdfPTable(5);
            float[] widths={1.5f, 1.5f, 1.5f, 1.5f, 1.5f};
            t.setWidths(widths);
            t.setTotalWidth(100);
            t.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            PdfPCell c1 = new PdfPCell(new Paragraph("PO#", headerFont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Line#", headerFont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Part#", headerFont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Description#", headerFont));
            t.addCell(c1);
            c1 = new PdfPCell(new Paragraph("Origin#", headerFont));
            t.addCell(c1);
             
            //calculate the real records within a page ,to calculate the last record number of every page
            int maxRecordInPage= j+1 ==totalPage ? (remainPage==0?recordPerPage:(po.size()%recordPerPage)):recordPerPage;
             
            for(int i=j*recordPerPage;i<((j*recordPerPage)+maxRecordInPage);i++){
            	PdfPCell c2=new PdfPCell(new Paragraph(po.get(i), bodyFont));
                t.addCell(c2);
                c2=new PdfPCell(new Paragraph(line.get(i), bodyFont));
                t.addCell(c2);
                c2=new PdfPCell(new Paragraph(part.get(i), bodyFont));
                t.addCell(c2);
                c2=new PdfPCell(new Paragraph(description.get(i), bodyFont));
                t.addCell(c2);
                c2=new PdfPCell(new Paragraph(origin.get(i), bodyFont));
                t.addCell(c2);
            }
            document.add(t);
             
            if(j+1==totalPage){
                Paragraph foot11 = new Paragraph("文件只作  Foster 收货用"+printBlank(150)+"__________________________", headerFont);
                document.add(foot11);
                Paragraph foot12 = new Paragraph("Printed from Foster supplier portal"+printBlank(124)+company+printBlank(40)+"版本: 1.0", headerFont);
                document.add(foot12);
            }
        }
        document.close();
        System.out.println("生成PDF文档完成");
    }
     
    // 模拟往表格里填充内容
    private static void add(List<String> list, String suffix, int num){
        for(int i=1;i<=num;i++){
            list.add(suffix + i);
        }
    }
     
    // 打印空白单元格
    private static String printBlank(int tmp){
          String space="";
          for(int m=0;m<tmp;m++){
              space=space+" ";
          }
          return space;
    }
 
}